<?php
$ruta = 'pluginsbot/';
include($ruta.'Greeting.php');
include($ruta.'Say.php');
include($ruta.'Entra.php');
include($ruta.'Quit.php');
include($ruta.'Modo.php');
include($ruta.'Traduce.php');
include($ruta.'Rand.php');
include($ruta.'Ayuda.php');

$config = array(
	'server' => 'irc.freenode.net',
	'port' => 6667,
	'nick' => 'Master-Mind',
	'canal' => '#madhacking',
	'admin' => 'MadPitbull'
);
$socket = true;

class IRCBot {
	var $socket;
	var $expl = array();
	var $data;
	
	function __construct($config, $socket) {
		$this->socket = $socket;
		
		$this->socket = fsockopen($config['server'], $config['port']);
		$this->login($config);
		$this->inicio($config);
	}
	
	function login($config) {
		$this->enviar_dat('USER', $config['nick'].' sourcegeek.com '.$config['nick'].' :'.$config['nick']);
		$this->enviar_dat('NICK', $config['nick']);
		$this->enviar_dat('JOIN', $config['canal']);
	}
	
	function inicio($config) {
		$data = fgets($this->socket, 256);
		$this->data = $data;
		echo $data;
		flush();
		$this->expl = explode(' ', $data); //1=PRIVMSG 2=Canal 3=Comando 4=Parametro
		$string = $this->expl;
		unset($string[0],$string[1],$string[2],$string[3]);
		$string = implode(' ',$string);
		
		if ($this->expl[0] == 'PING' || $this->expl[0] == "PING\r\n" || $this->expl[0] == "PING\n") {
			IRCBot::enviar_dat('PONG', $this->expl[1]);
			echo 'PONG '.$this->expl[1];
		}
		
		if ($this->expl[1] == 'KICK' && $this->expl[3] == $config['nick']) {
			$this->enviar_dat('JOIN', $this->expl[2]);
		}
		if ($this->expl[1] == 'JOIN') {
			$acc = new Greeting($this->socket, $this->expl, $config);
		}
		if ($this->expl[3] == ':!say') {
			$acc = new Say($this->socket, $this->expl, $string);
		}
		if ($this->expl[3] == ':!entra') {
			$acc = new Entra($this->socket, $this->expl, $config);
		}
		if ($this->expl[3] == ':!sal') {
			$acc = new Salir($this->socket, $this->expl, $config);
		}
		if (trim($this->expl[3]) == ':!quit') {
			$acc = new Quit($this->socket, $this->expl, $config);
		}
		if ($this->expl[3] == ':!modo') {
			$acc = new Modo($this->socket, $this->expl);
		}
		if($this->expl[3] == ':!traduce') {
			$acc = new Traduce($this->socket, $this->expl, $string);
		}
		if ($this->expl[3] == ':!rand') {
			$acc = new Rand($this->socket, $this->expl, $string);
		}
		if (trim($this->expl[3]) == ':!ayuda') {
			$acc = new Ayuda($this->socket, $this->expl, $config);
		}
		
		IRCBot::inicio($config);
	}
	
	function unsset() {
		$this->kick = false;
	}
	
	function enviar_dat($comand, $msg, $rn = true) {
		if ($rn == true) {
			fputs($this->socket, $comand.' '.$msg."\r\n");
		}else{
			fputs($this->socket, $comand.' '.$msg);
		}
	}
	
	function get_nick($array) {
		$nicka = explode('@', $array);
		$nickb = explode('!', $nicka[0]);
		$nickc = explode(':', $nickb[0]);
		return $nickc[1];
	}
}

$bot = new IRCBot($config, $socket);
?>